package info.gridworld.actor;

import info.gridworld.grid.Location;

public class ZBug extends Bug {
	
    private int steps;
    private int sideLength;
    
    //标志是否结束的旗标
    private boolean overFlag;
    
    //第几次旋转
    private int turn_time; 

    /**
     * Constructs a box bug that traces a square of a given side length
     * @param length the side length
     */
    public ZBug(int length)
    {
        steps = 0;
        turn_time = 1;
        overFlag = false;
        //a default ZBug needs to be facing east
        this.setDirection(Location.EAST);
        sideLength = length;
    }

    /**
     * Moves to the next location of the square.
     */
    public void act()
    {
    	if(overFlag) {
    		return;
    	}
    	
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
        	//如果是第一次旋转，则旋转135度
        	if(turn_time == 1) {
        		this.setDirection(getDirection() + Location.SOUTHEAST);
                turn_time++;
        	}
        	else if(turn_time == 2)
        	{ //如果是第二次旋转，则旋转225度
//        		this.removeSelfFromGrid();
        		this.setDirection(getDirection() + Location.SOUTHWEST);
        		turn_time++;
        	} else {
        		overFlag = true;
        	}
            steps = 0;
        }
    }
}
